#!/bin/bash
#
# Copyright (c) 2020-2023 NVI, Inc.
#
# This file is part of VLBI Field System
# (see http://github.com/nvi-inc/fs).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

VERSION=2023-06-12

version(){
    echo "[fesh $VERSION]"
}

#enable 'strict' mode
set -euo pipefail

FS_BASE=${FS_BASE:-'/usr2'}
FS_DIR=${FS_DIR:-"${FS_BASE}/fs"}
CONTROL_DIR=${CONTROL_DIR:-"${FS_BASE}/control"}
LOG_DIR=${LOG_DIR:-"${FS_BASE}/log"}
# check for skedf.ctl
if [[ -e $CONTROL_DIR/skedf.ctl ]]; then
    proc_dir=$( perl -ne 'END {if(eof) {$_=".";} print;} if(/^\$proc/i     ) {while (<>){if(/^\$/) {$_=".";exit;} elsif(/^[ \t]*([^* \t\n].*?)\/*[ \t\r]*$/) {$_=$1;exit;}}}' <$CONTROL_DIR/skedf.ctl)
    snap_dir=$( perl -ne 'END {if(eof) {$_=".";} print;} if(/^\$snap/i     ) {while (<>){if(/^\$/) {$_=".";exit;} elsif(/^[ \t]*([^* \t\n].*?)\/*[ \t\r]*$/) {$_=$1;exit;}}}' <$CONTROL_DIR/skedf.ctl)
    sched_dir=$(perl -ne 'END {if(eof) {$_=".";} print;} if(/^\$schedules/i) {while (<>){if(/^\$/) {$_=".";exit;} elsif(/^[ \t]*([^* \t\n].*?)\/*[ \t\r]*$/) {$_=$1;exit;}}}' <$CONTROL_DIR/skedf.ctl)
    PROC_DIR=${PROC_DIR:-"${proc_dir}"}
    SNAP_DIR=${SNAP_DIR:-"${snap_dir}"}
    SCHED_DIR=${SCHED_DIR:-"${sched_dir}"}
#do not use the above the three *_DIR environment variables
#to override what is in skedf.ctl; they can be used to override
#what is placed in the corresponding *_dir variables if the
#perl directory extracts get the wrong result
    LIST_DIR=${LIST_DIR:-${SCHED_DIR}}
else
# drudg won't run without skedf.ctl, maybe some day it will
# we can still get the schedule
# use current directory without other information
    SCHED_DIR=${SCHED_DIR:-"."}
fi

FESH_DATA_CENTER=${FESH_DATA_CENTER:-'cddis'}

FESH_CDDIS_METHOD=${FESH_CDDIS_METHOD:-'ftp-ssl'}
FESH_BKG_METHOD=${FESH_BKG_METHOD:-'ftp-ssl'}

if [[ ! -z "${FESH_GEO_TPICD:-}" ]]; then
    tpicd=$FESH_GEO_TPICD
fi
if [[ ! -z "${FESH_GEO_CONT_CAL:-}" ]]; then
    cont_cal=$FESH_GEO_CONT_CAL
fi
if [[ ! -z "${FESH_GEO_CONT_CAL_POLARITY:-}" ]]; then
    cont_cal_polarity=$FESH_GEO_CONT_CAL_POLARITY
fi
if [[ ! -z "${FESH_GEO_VSI_ALIGN:-}" ]]; then
    vsi_align=$FESH_GEO_VSI_ALIGN
fi
if [[ ! -z "${FESH_GEO_USE_SETUP_PROC:-}" ]]; then
    use_setup_proc=$FESH_GEO_USE_SETUP_PROC
fi
if [[ ! -z "${FESH_GEO_VDIF_SINGLE_THREAD_PER_FILE:-}" ]]; then
    vdif_single_thread_per_file=$FESH_GEO_VDIF_SINGLE_THREAD_PER_FILE
fi
if [[ -z "${NETRC_DIR:-}" ]]; then
    NETRC_DIR=$HOME
    CURL_NETRC_FILE_OPTION=
else
    CURL_NETRC_FILE_OPTION=" --netrc-file $NETRC_DIR/.netrc"
fi
if [[ ! -z "${STATION:-}" ]]; then
    station=$STATION
fi

fatal(){
    echo -e "ERROR: fesh:" "$*" >&2
    exit 1
}

year=$(date +%Y)
month=$(LANG=C date +%b%y|tr [:upper:] [:lower:])

usage() {
    cat <<EOF
Usage: $0 [options] schedule
Fetch schedule file

Options:
 -n       Fetch next schedule in master file (not supported yet)
 -d       drudg schedule with default settings, not supported for vlbeer
          (needs STATION environment variable or -s set with two letter id)
 -D dc    Use alternate data center; dc=bkg, cddis, opar, or vlbeer. Upper
          and lower case are accepted. (see more below about setting the
          default data center)
 -v       Verbose mode: curl/wget verbose and drudg output printed to stdout
 -V       Print version and exit.
 -P       With -d, use drudg to send the schedule summary to the printer
 -S       Skip download (including for -x and -f)
 -m mmmyy Specify three character month and two digit year for the vlbeer
          data center (see more below about specifying the month/year)
          uses the hidden .latest sub-directory by default, use -H to suppress
 -H       suppress use of the hidden .latest sub-directory for vlbeer
 -y yyyy  Specify different (four-digit) year than the current one
          for geo data centers (see more below about specifying the year)
 -f       Force downloading of schedule (and .txt witb -x) files from server
          (if -S is not used) and force re-drudging when -d is used, otherwise
          existing files will not be replaced
 -x       download corresponding .txt file as well (geo only)
 -s xx    Override STATION environment variable
 -a val   Override drudg VSI_ALIGN answer to val
 -c val   Override drudg CONT_CAL answer to val
 -p val   Override drudg CONT_CAL_POLARITY answer to val
 -t val   Override drudg TPICD answer to val
 -T val   Override drudg VDIF_SINGLE_THREAD_PER_FILE answer to val
 -u val   Override drudg SETUP_PROC answer to val
 -h       Print this message

NOTE: If you specify a schedule that doesn't exist, you will get a
file or a directory error from the server.

For the '-d' option, the STATION environment variable, or the -s
option, must be set with the two-letter station code. For example, for
STATION:

    setenv STATION gs #in ~/.login for tcsh or
    export STATION=gs #in ~/.profile for bash

If both are set, -s overrides STATION. In any event, the value is
mapped to lower case, if it isn't already, for agreement with drudg.

Please see the end of this message about handling optional 'skedf.ctl'
specified prompting for 'drudg'.

The '-d' option is not supported for data center 'vlbeer'.

The default data center is CDDIS but can be changed by setting the
FESH_DATA_CENTER environment variable. Accepted values are bkg, cddis,
opar (the three 'geo' centers) or vlbeer. The value is mapped to lower
case, if it isn't already. For example, to make the default bkg, use:

    setenv FESH_DATA_CENTER bkg #in ~/.login for tcsh or
    export FESH_DATA_CENTER=bkg #in ~/.profile for bash

The '-D' option can be used to specify an alternate data center when
running the script.  If the data center is vlbeer, the '-d' option
cannot be used.

For geo data centers, fesh by default gets a schedule for the current
year, but this can be overridden with '-y yyyy' option to specify a
four digit year 'yyyy'. That option can be used to get a schedule for
the next year. The 'yyyy' is not checked for validity and the
download will fail if it is not correct.

For vlbeer, fesh by default gets a schedule from the hidden .latest
sub-directory for the current month, but this can be overridden with
'-m mmmyy' to specify a three character month abbreviation 'jan',
'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov',
'dec' as 'mmm' and a two digit year 'yy'. The hidden .latest
sub-directory of the specified month will be used.  The adding of
/.latest to the current or '-m' specified directory can be suppressed
the -H option.  The 'mmmyy' is not checked for validity and the
download will fail if it is not correct.

fesh (curl actually) will prompt for an email to use as the password
for anonymous ftp-ssl access for CDDIS. The prompt can be suppressed
by setting a working email address in the FESH_EMAIL variable, e.g.:

    setenv FESH_EMAIL user@address #in ~/.login for tcsh or
    export FESH_EMAIL=user@address #in ~/.profile for bash

The directories for .skd, .snp, and .prc files are obtained from
the skedf.ctl control file. The listing files go with the .skd files
by default, but can be over-ridden with the LIST_DIR environment
variable. For example to make it the current directory, use:

    setenv LIST_DIR . #in ~/.login for tcsh or
    export LIST_DIR=. #in ~/.profile for bash

If the -x option is used to download the .txt file (for geo schedules
only), the file is placed in the same directory as the .skd file.

The default transfer method from CDDIS is ftp-ssl.  You can use https
as the method with CDDIS if you set:

    setenv FESH_CDDIS_METHOD https #in ~/.login for tcsh or
    export FESH_CDDIS_METHOD=https #in ~/.profile for bash

For the CDDIS https method, the login must be configured in ".netrc"
which by default is in "~".  For example:

    machine urs.earthdata.nasa.gov
        login mycddisuser
        password secret

See "man 5 netrc" for more details on ".netrc".

If your curl supports the "--netrc-file" option (see "man curl") , you
can change the directory used for ".netrc" by setting the NETRC_DIR
environment variable.  This matches plog. E.g., add the following to
your login script

    setenv NETRC_DIR "/usr2/control" #in ~/.login for tcsh or
    export NETRC_DIR="/usr2/control" #in ~/.profile for bash

If you have FSL8 or another old distribution that does not support
curl ftp-ssl or https with CDDIS, but worked with the previous
versions of fesh that used wget for non-ssl ftp, you can restore the
previous behaviour with:

    setenv FESH_CDDIS_METHOD ftp #in ~/.login for tcsh or
    export FESH_CDDIS_METHOD=ftp #in ~/.profile for bash

However, this method will no longer work when CDDIS stops support
of non-ssl ftp, which is expected at the end of October 2020.

The default transfer method from BKG is ftp-ssl.  If you have FSL8 or
another old distribution that does not support curl ftp-ssl
with BKG, but worked with the previous versions of fesh that used wget
for non-ssl ftp, you can restore the previous behaviour with:

    setenv FESH_BKG_METHOD ftp #in ~/.login for tcsh or
    export FESH_BKG_METHOD=ftp #in ~/.profile for bash

However, this method will no longer work when BKG stops support
of non-ssl ftp, which is expected at the end of May 2022.

With the '-d' option, if the 'skedf.ctl' control file specifies
prompting for any of: tpicd, vsi_align, cont_cal, cont_cal_polarity,
using setup_proc, or single VDIF thread per file, a fixed answer can
be provided by setting the environment variables: FESH_GEO_TPICD,
FESH_GEO_VSI_ALIGN, FESH_GEO_CONT_CAL, FESH_GEO_CONT_CAL_POLARITY,
FESH_GEO_USE_SETUP_PROC, and FESH_GEO_VDIF_SINGLE_THREAD_PER_FILE
respectively. These settings can be overridden when running the script
with the options: '-t', '-a', '-c', '-p', '-u', and '-T' respectively.
Using '' as the value for an option will unset the corresponding value
supplied as an environment variable. Please be sure to read the
follow four NOTEs:

  NOTE: If used incorrectly, this feature can fail in complicated ways
  with no obvious indication. Use at your own risk. Simple use with
  single mode schedules should be fine.

  NOTE: If there is more than mode in the schedule, it is not possible
  to supply answers in this way. 'drudg' will have to be run manually.

  NOTE: If any of these environment variables or options are used, the
  corresponding prompts must be enabled in 'skedf.ctl'. If there isn't
  agreement, incorrect .prc files may be created without an obvious
  indication of a problem. Be careful in particular of
  FESH_GEO_VSI_ALIGN and the '-a' option since the corresponding
  prompt will only occur if a DBBC3 PFB mode is in use.

  NOTE: To provide minimal protection, the script will reject:

   -setting an answer for vsi_align and answers either for cont_cal and/or
    cont_cal_polarity
   -setting an answer for cont_cal_polarity if the answer for cont_cal is off
   -setting the tpicd answer to anything but a non-negative integer
   -setting the cont_cal answer to anything but on or off
   -setting the cont_cal_polarity to answer anything but 0, 1, 2, 3, or none
   -setting the vsi_align answer to anything but 0, 1, or none
   -setting the use_setup_proc answer to anything but yes or no
   -setting the vdif_single_thread_per_file answer to anything but yes or no
EOF
}

run_drudg=
force=
verbose=
txt=
print=
download=1
while getopts 'a:c:nfdD:hHm:p:Ps:St:T:u:vVxy:' opt; do
    case $opt in
        n)
            echo "'next' not implemented."
            exit 1
            ;;
        d)
            if [[ ! -e $CONTROL_DIR/skedf.ctl ]]; then
                echo "$CONTROL_DIR/skedf.ctl doesn't exist, can't run drudg, don't use '-d'" >&2
                exit 1
            fi
            run_drudg=1
            ;;
        D)
            FESH_DATA_CENTER=$OPTARG
            ;;
        f)
            force=1
            ;;
        m)
            month=$OPTARG
            month_option=1
            ;;
        H)
            not_hidden=1
            ;;
        y)
            year=$OPTARG
            year_option=1
            ;;
        h)
            usage
            exit 0
            ;;
        v)
            verbose=1
            ;;
        V)
            version
            exit 0
            ;;
        x)
            txt=1
            ;;
        P)
            print=1
            ;;
        S)
            download=
            ;;
        s)
            station=$OPTARG
            ;;
        t)
            if [ ! -z ${OPTARG} ]; then tpicd=$OPTARG; fi
            ;;
        c)
            if [ ! -z ${OPTARG} ]; then cont_cal=$OPTARG; fi
            ;;
        p)
            if [ ! -z ${OPTARG} ]; then cont_cal_polarity=$OPTARG; fi
            ;;
        a)
            if [ ! -z ${OPTARG} ]; then vsi_align=$OPTARG; fi
            ;;
        u)
            if [ ! -z ${OPTARG} ]; then use_setup_proc=$OPTARG; fi
            ;;
        T)
            if [ ! -z ${OPTARG} ]; then vdif_single_thread_per_file=$OPTARG; fi
            ;;
        *)
            echo "Try the '-h' option for help" >&2
            exit 1
    esac
done
shift $((OPTIND - 1))

if [[ $# -eq 0 ]] ; then
    usage >&2
    exit 1
fi

sched=$1

FESH_DATA_CENTER=$(echo "$FESH_DATA_CENTER" | tr '[:upper:]' '[:lower:]')
if [[ ! "$FESH_DATA_CENTER" =~ ^(cddis|opar|bkg|vlbeer)$ ]]; then
   echo "Unknown data center: $FESH_DATA_CENTER."
   exit 1
fi
if [[ "$FESH_DATA_CENTER" =~ vlbeer ]]; then
    if [[ -n "$txt" ]]; then
        echo "Option -x (.txt) is not supported for the vlbeer data center."
        echo "To fetch the schedule, rerun without -x."
        exit 1
    elif [[ -n "$run_drudg" ]]; then
        echo "Option -d (drudg) is not supported for the vlbeer data center."
        echo "To fetch the schedule, rerun without -d."
        exit 1
    fi
    if [[ ! -z  "${year_option:-}" ]]; then
        echo "The -y option can't be used for the vlbeer data center"
        exit 1
    fi
    if [[ -z  "${not_hidden:-}" ]]; then
        month="${month}/.latest"
    fi
elif [[ ! -z "${month_option:-}" ]]; then
        echo "The -m option can't be used for a geo data center"
        exit 1
fi

if [[ ! "$FESH_CDDIS_METHOD" =~ ^(ftp|ftp-ssl|https)$ ]]; then
   echo "Unknown cddis method: $FESH_CDDIS_METHOD."
   exit 1
fi

if [[ ! "$FESH_BKG_METHOD" =~ ^(ftp|ftp-ssl)$ ]]; then
   echo "Unknown bkg method: $FESH_BKG_METHOD."
   exit 1
fi

# build optional answers
tpicd_answer=
if [ ! -z ${tpicd+x} ]; then tpicd_answer=$tpicd$'\n'; fi
cont_cal_answer=
if [ ! -z ${cont_cal+x} ]; then cont_cal_answer=$cont_cal$'\n'; fi
cont_cal_polarity_answer=
if [ ! -z ${cont_cal_polarity+x} ]; then cont_cal_polarity_answer=$cont_cal_polarity$'\n'; fi
vsi_align_answer=
if [ ! -z ${vsi_align+x} ]; then vsi_align_answer=$vsi_align$'\n'; fi
use_setup_proc_answer=
if [ ! -z ${use_setup_proc+x} ]; then use_setup_proc_answer=$use_setup_proc$'\n'; fi
vdif_single_thread_per_file_answer=
if [ ! -z ${vdif_single_thread_per_file+x} ]; then vdif_single_thread_per_file_answer=$vdif_single_thread_per_file$'\n'; fi

#prevent simple errors in answers
if [[ ! "$tpicd_answer" =~ ^([0-9]+$'\n'|)$ ]]; then
   echo -n "tpicd must be a non-negative integer or not set, was $tpicd_answer"
   exit 1
fi
if [[ ! "$cont_cal_answer" =~ ^(on$'\n'|off$'\n'|)$ ]]; then
   echo -n "CONT_CAL must be on, off, or not set, was $cont_cal_answer"
   exit 1
fi
if [[ ! "$cont_cal_polarity_answer" =~ ^([0-3]+$'\n'|none$'\n'|)$ ]]; then
   echo -n "CONT_CAL_POLARITY must be 0, 1, 2, 3, none, or not set, was $cont_cal_polarity_answer"
   exit 1
fi
if [[ ! "$vsi_align_answer" =~ ^([0-1]+$'\n'|none$'\n'|)$ ]]; then
   echo -n "VSI_ALIGN must be 0, 1, none, or not set, was $vsi_align_answer"
   exit 1
fi
if [[ ! "$use_setup_proc_answer" =~ ^(yes$'\n'|no$'\n'|)$ ]]; then
   echo -n "USE_SETUP_PROC must be yes, no, or not set, was $use_setup_proc_answer"
   exit 1
fi
if [[ ! "$vdif_single_thread_per_file_answer" =~ ^(yes$'\n'|no$'\n'|)$ ]]; then
   echo -n "VDIF_SINGLE_THREAD_PER_FILE must be yes, no, or not set, was $vdif_single_thread_per_file_answer"
   exit 1
fi
if [ ! -z "$vsi_align_answer"  ]
then
    if [ ! -z "$cont_cal_answer" ]
    then
        if [ ! -z "$cont_cal_polarity_answer" ]
        then
            echo "setting VSI_ALIGN is not compatible with setting CONT_CAL and CONT_CAL_POLARITY"
            exit 1
        fi
        echo "setting VSI_ALIGN is not compatible with setting CONT_CAL"
        exit 1
    fi
    if [ ! -z "$cont_cal_polarity_answer" ]
    then
        echo "setting VSI_ALIGN is not compatible with setting CONT_CAL_POLARITY"
        exit 1
    fi
fi
if [[ "$cont_cal_answer" =~ off  ]]; then
    if [ ! -z "$cont_cal_polarity_answer" ]
    then
        echo "setting CONT_CAL_POLARITY with CONT_CAL off is not supported"
        exit 1
    fi
fi

## Conflict check
if [[ "$FESH_DATA_CENTER" =~ vlbeer ]]; then
    if [[ -e $SCHED_DIR/$sched.vex ]]; then
        if [[ -z "$force" ]]; then
            echo "$SCHED_DIR/$sched.vex exists, delete or use '-f' to get fresh schedule" >&2
            exit 1
        fi
        rm "$SCHED_DIR/$sched.vex"
    fi
elif [[ -n "$download" ]]; then
    if [[ -e $SCHED_DIR/$sched.skd ]]; then
        if [[ -z "$force" ]]; then
            echo "$SCHED_DIR/$sched.skd exists, delete or use '-f' to get fresh schedule" >&2
            exit 1
        fi
        rm "$SCHED_DIR/$sched.skd"
    fi
    if [[ -n "$txt" ]]; then
        if [[ -e $SCHED_DIR/$sched.txt ]]; then
            if [[ -z "$force" ]]; then
                echo "$SCHED_DIR/$sched.txt exists, delete or use '-f' to get fresh schedule" >&2
                exit 1
            fi
            rm "$SCHED_DIR/$sched.txt"
        fi
    fi
fi

if [[ -n "$run_drudg" ]]; then
    if [[ ! -n "$download" ]]; then
        if [[ ! -e $SCHED_DIR/$sched.skd ]]; then
            echo "$SCHED_DIR/$sched.skd doesn't exist" >&2
            exit 1
        fi
    fi
    set +u
    if [[ -z "$station" ]]; then
        >&2 echo "$0: ERROR: Neither STATION environment variable nor -s are set; should be set to two-letter station code if using drudg."
        exit 1
    fi
    set -u

#force lower case station code
    station=$(echo "$station" | tr '[:upper:]' '[:lower:]')

    if [[ -e $SNAP_DIR/$sched$station.snp ]]; then
        if [[ -z "$force" ]]; then
            echo "$SNAP_DIR/$sched$station.snp exists, delete or use '-f' to re-drudg" >&2
            exit 1
        fi
        rm "$SNAP_DIR/$sched$station.snp"
    fi
    if [[ -e $PROC_DIR/$sched$station.prc ]]; then
        if [[ -z "$force" ]]; then
            echo "$PROC_DIR/$sched$station.prc exists, delete or use '-f' to re-drudg" >&2
            exit 1
        fi
        rm "$PROC_DIR/$sched$station.prc"
    fi
    if [[ -e $LIST_DIR/$sched$station.lst ]]; then
        if [[ -z "$force" ]]; then
            echo "$LIST_DIR/$sched$station.lst exists, delete or use '-f' to re-drudg" >&2
            exit 1
        fi
        rm "$LIST_DIR/$sched$station.lst"
    fi
fi
 
if [[ -z "${FESH_EMAIL:-}" ]]; then
    user=anonymous
else
    user="anonymous:$FESH_EMAIL"
fi
version
if [[ -n "$download" ]]; then
    if [[ -n "$verbose" ]]; then
        CURL_VERBOSE=-v
        WGET_VERBOSE=
    else
        WGET_VERBOSE=-nv
        CURL_VERBOSE=
    fi
    pushd "$SCHED_DIR" >/dev/null
    echo Fetching schedule from server... >&2
    #TODO: if not found, maybe check next or previous years?
    if [[ "$FESH_DATA_CENTER" =~ cddis ]]; then
        if [[ "$FESH_CDDIS_METHOD" =~ ^ftp$ ]]; then
            wget $WGET_VERBOSE "ftp://ftp@cddis.gsfc.nasa.gov/vlbi/ivsdata/aux/$year/$sched/$sched.skd"
            if [[ -n "$txt" ]]; then
                wget $WGET_VERBOSE "ftp://ftp@cddis.gsfc.nasa.gov/vlbi/ivsdata/aux/$year/$sched/$sched.txt"
            fi
        elif [[ "$FESH_CDDIS_METHOD" =~ ^https$ ]]; then
            if [[ ! -e "$NETRC_DIR/.netrc" ]]; then
                fatal "$NETRC_DIR/.netrc not found, see usage"
            fi

            if ! grep -q urs.earthdata.nasa.gov "$NETRC_DIR/.netrc"; then
                fatal "$NETRC_DIR/.netrc does not contain CDDIS login information, see usage"
            fi
            curl  -k  -f -c /dev/null $CURL_VERBOSE -n \
                $CURL_NETRC_FILE_OPTION \
                -L -O "https://cddis.nasa.gov/archive/vlbi/ivsdata/aux/$year/$sched/$sched.skd"
            if [[ -n "$txt" ]]; then
                curl  -k  -f -c /dev/null $CURL_VERBOSE -n \
                    $CURL_NETRC_FILE_OPTION \
                    -L -O "https://cddis.nasa.gov/archive/vlbi/ivsdata/aux/$year/$sched/$sched.txt"
            fi
        else
            curl -u "$user" -k -O $CURL_VERBOSE --ftp-ssl "ftp://gdc.cddis.eosdis.nasa.gov/vlbi/ivsdata/aux/$year/$sched/$sched.skd"

            if [[ -n "$txt" ]]; then
                curl -u "$user" -k -O $CURL_VERBOSE --ftp-ssl "ftp://gdc.cddis.eosdis.nasa.gov/vlbi/ivsdata/aux/$year/$sched/$sched.txt"
            fi
        fi
    elif [[ "$FESH_DATA_CENTER" =~ opar ]]; then
        wget $WGET_VERBOSE "ftp://ftp@ivsopar.obspm.fr/vlbi/ivsdata/aux/$year/$sched/$sched.skd"
        if [[ -n "$txt" ]]; then
            wget $WGET_VERBOSE "ftp://ftp@ivsopar.obspm.fr/vlbi/ivsdata/aux/$year/$sched/$sched.txt"
        fi
    elif [[ "$FESH_DATA_CENTER" =~ bkg ]]; then
        if [[ "$FESH_BKG_METHOD" =~ ^ftp$ ]]; then
            wget $WGET_VERBOSE "ftp://ftp@ivs.bkg.bund.de/pub/vlbi/ivsdata/aux/$year/$sched/$sched.skd"
            if [[ -n "$txt" ]]; then
                wget $WGET_VERBOSE "ftp://ftp@ivs.bkg.bund.de/pub/vlbi/ivsdata/aux/$year/$sched/$sched.txt"
            fi
        else
            curl --ssl-reqd $CURL_VERBOSE -u anonymous:anonymous -O "ftp://ivs.bkg.bund.de/pub/vlbi/ivsdata/aux/$year/$sched/$sched.skd"
            if [[ -n "$txt" ]]; then
                curl --ssl-reqd $CURL_VERBOSE -u anonymous:anonymous -O "ftp://ivs.bkg.bund.de/pub/vlbi/ivsdata/aux/$year/$sched/$sched.txt"
            fi
        fi
    elif [[ "$FESH_DATA_CENTER" =~ vlbeer ]]; then
        wget $WGET_VERBOSE "ftp://ftp@vlbeer.ira.inaf.it/vlb_arc/ftp/vlbi_arch/$month/$sched.vex"
    fi
    echo Done >&2
    popd >/dev/null
fi

if [[ -n "$run_drudg" ]]; then
    nine=$'9'
    twelve=$'12'

    drudg_out="/dev/null"
    if [[ -n "$verbose" ]]; then
        drudg_out="/dev/stdout"
    fi

    if [[ -n "$print" ]]; then
        print=$'5'$'\n'
        echo "Will send summary to printer" >&2
    fi
    echo -n Drudging schedule... >&2
    cat <<EOF | "$FS_DIR/bin/drudg" "$sched.skd" > "$drudg_out"
$station
3
$use_setup_proc_answer$twelve
$tpicd_answer$vdif_single_thread_per_file_answer$cont_cal_answer$cont_cal_polarity_answer$vsi_align_answer$print$nine
$LIST_DIR/$sched$station.lst



5
0
EOF

    echo Done. >&2
fi
